/*
 Project Milestone 3
 by Lorenzo Miori [Lorenzo.Miori@stud-inf.unibz.it,
 Matteo Angriman [Matteo.Angriman@stud-inf.unibz.it],
 Nick Preda [Nicolae.Preda@stud-inf.unibz.it]
 */
package GUI;

import DB.BookingDB;
import DB.DatabaseLogin;
import DB.HotelDB;
import java.awt.Component;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import javax.swing.JOptionPane;

/**
 *
 * @author Nick
 */
public class GeneralBookingGUI extends javax.swing.JFrame {

    private Component parentComponent;
    private ArrayList<HashMap<String, String>> hotels;

    public GeneralBookingGUI(Component parentComponent) {
        this.parentComponent = parentComponent;
        initComponents();
        prepareData();
        setVisible(true);
    }

    private void prepareData() {
        hotels = new HotelDB(DatabaseLogin.getLastConnection()).getTableData();
        hotelChooser.removeAllItems();
        /* Fill the drop menu */
        for (HashMap<String, String> addr : hotels) {
            hotelChooser.addItem(HotelDB.formatHotel(addr));
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        hotelChooser = new javax.swing.JComboBox();
        fromDate = new com.toedter.calendar.JCalendar();
        jLabel4 = new javax.swing.JLabel();
        toDate = new com.toedter.calendar.JCalendar();
        CancelButton = new javax.swing.JButton();
        OKButton = new javax.swing.JButton();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Hotel Bookings State");

        jLabel2.setText("Choose Hotel: ");

        hotelChooser.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel4.setText("Choose Dates From and To");

        CancelButton.setText("Cancel");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        OKButton.setText("OK");
        OKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(hotelChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(273, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(fromDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(CancelButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(toDate, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(OKButton, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(28, 28, 28))))
            .addGroup(layout.createSequentialGroup()
                .addGap(221, 221, 221)
                .addComponent(jLabel1)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hotelChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(toDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(fromDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(CancelButton)
                            .addComponent(OKButton))
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        /* Close current view */
        setVisible(false);
        dispose();
        /* Enable previous view */
        parentComponent.setEnabled(true);
        parentComponent.setVisible(true);
    }//GEN-LAST:event_CancelButtonActionPerformed

    private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed

        /*
        int nr = Integer.parseInt(jFormattedTextField1.getValue().toString());
        if (nr <= 0) {
            JOptionPane.showMessageDialog(this, "Please select an amount of people greater than 0!", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        */
        Date from = fromDate.getDate();
        Date to = toDate.getDate();
        int hotelID = Integer.parseInt(hotels.get(hotelChooser.getSelectedIndex()).get(HotelDB.HOTEL_ID));

        /* Do a nice query */

        // TODO

        ArrayList<HashMap<String, String>> output = new ArrayList<HashMap<String, String>>();
        PreparedStatement st = null;
        ResultSet rs = null;
        ResultSetMetaData rsmd;
        try {
            st = DatabaseLogin.getLastConnection().prepareStatement("select * from Booking "
                    + "where booking_id in (select booking_id from RoomBookingRelation where hotel_id = ?) "
+ "AND (? BETWEEN book_from_date AND book_to_date OR ? BETWEEN book_from_date AND book_to_date OR book_from_date BETWEEN ? AND ? OR book_to_date BETWEEN ? AND ?)");
            st.setInt(1, hotelID);
            st.setDate(2, new java.sql.Date(from.getTime()));
            st.setDate(3, new java.sql.Date(to.getTime()));
            st.setDate(4, new java.sql.Date(from.getTime()));
            st.setDate(5, new java.sql.Date(to.getTime()));
            st.setDate(6, new java.sql.Date(from.getTime()));
            st.setDate(7, new java.sql.Date(to.getTime()));
            /* SELECT */
            rs = st.executeQuery();
            while (rs.next()) {
                HashMap<String, String> newEntry = new HashMap<String, String>();
                rsmd = rs.getMetaData();
                for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                    newEntry.put(rsmd.getColumnName(i), rs.getString(i));
                }
                output.add(newEntry);
            }
            System.out.println(output);

        } catch (SQLException ex) {
            System.err.println(ex.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }

            } catch (SQLException ex) {
                System.err.println(ex.getMessage());
            }
        }
        
        String text = "";
        for (HashMap<String, String> z : output) {
            text+= BookingDB.formatBooking(z);
            text+="\n";
        }
        JOptionPane.showMessageDialog(this, output.size()+" bookings found!\nDETAILS:\n"+text);;
        
    }//GEN-LAST:event_OKButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        CancelButtonActionPerformed(null);
    }//GEN-LAST:event_formWindowClosing
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JButton OKButton;
    private com.toedter.calendar.JCalendar fromDate;
    private javax.swing.JComboBox hotelChooser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private com.toedter.calendar.JCalendar toDate;
    // End of variables declaration//GEN-END:variables
}
